<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- 排课 -->
<div class="" style="padding: 5px;"> 
	<div style="float:right;">
		  <input type="text" id="serch_text_teacher" style="width:250px" placeholder="用户名，显示名，电话，英文名">
	</div>
</div>
<div style="height:20px;line-height:20px;">
	当前老师：<b id="teacher_class"  teacherId ="${param.table_id}">${param.userShowName}</b>
</div>
<div id="classPlant_tabs" style="margin-top:20px;">
		 
</div>
<div style="padding: 5px;">
	<div style="float:right;">
		   <button id="saveClassplant" class="btn btn-lg btn-success">保存此老师课排表</button>
	</div>
</div>
<script>
(function(){
	var timelist= new Array();
	var id="${param.table_id}";
	//查询老师在哪些学校
	function loadingTachaerClas(){
		$("#classPlant_tabs").html("");
		var url ="${pageContext.request.contextPath}/edu/sys_ClassPlantAction.do?method=getTeacherClass";
		$.ajax({
			   type: "POST",
			   url: url,
			   async:false,
			   dataType:'json',
			   data: {table_id:id},
			   success: function(msg){ 
			      if(msg && msg.list && msg.list[0]!=null){ 
			    	  showMsg(msg);
			      }
			   }
		});
	};
	//显示tabs
	function showMsg(msg){
		 var ul=$("<ul></ul>");
		 var table_id="";
		 var timeLearn="";
    	 for(var i=0;i<msg.list.length;i++){
    		 if(msg.list[i].type=="4" || msg.list[i].type=="3"){
    			 continue;
    		 }
    		 
    		 if(table_id==""){
    			 table_id = msg.list[i].table_id;
    			 timeLearn = msg.list[i].timeLearn;
    		 }
    		  var li =$('<li id="li'+msg.list[i].table_id+'"><a href="#div'+msg.list[i].table_id+'" >'+msg.list[i].name+'</a></li>');
    		  ul.append(li); 
    		  $(li).attr("table_id",msg.list[i].table_id); 
    		  $(li).click(function(){
    				$("button[name=classbutton]").children().removeClass("icon-ok"); 
    			  	$("#teacher_class").attr('tid',"");
					$("#teacher_class").attr('value',"");
    		  });
    	 }
    	 $("#classPlant_tabs").append(ul);  
    	 for(var i=0;i<msg.list.length;i++){ 
    		 //找到所有学校
    		 if(msg.list[i].type!="2"){
    			 continue;
    		 }  
    		  var cid = msg.list[i].cid;
    		  var div=$('<div id="div'+msg.list[i].table_id+'"></div>');   		  
    		  $("#classPlant_tabs").append(div); 
    		  for(var j=0;j<msg.list.length;j++){
    			//打到学校下的年级
      			if(msg.list[j].type!="3"){
      				continue;
      			}
      			if(msg.list[j].cpid == cid){
      				var lvcid =msg.list[j].cid;//拿到年级的ID
      				for(var k=0;k<msg.list.length;k++){
      					if(msg.list[k].type!="4"){
      						continue;
      					}  
      					if(msg.list[k].cpid == lvcid){
      						var button =$('<button name="classbutton" class="btn btn-sm btn-success"><i></i>'+msg.list[k].pName+'</button>');
      						div.append(button);
      						button.attr("table_id",msg.list[k].table_id); 
      						div.append('&nbsp;&nbsp;&nbsp;');
      						button.click(function(){ 
      							$("button[name=classbutton]").children().removeClass("icon-ok"); 
      							$(this).children().addClass("icon-ok");
      							$("#teacher_class").attr('tid',$(this).attr("table_id"));
      							$("#teacher_class").attr('value',$(this).text());
      						})
      					}
      				}
      			}  
      		 } 
    	 }  
    	 $("#classPlant_tabs").tabs(); 
    	 //加载第一个老师的教室
    	 for(var i=0;i<msg.list.length;i++){
    		 if(msg.list[i].type!="2"){
    			 continue;
    		 }
    		 var table_id =msg.list[i].table_id;
    		 if(msg.list[i].timeLearn.length<5){
    			 continue;
    		 }
    		 var arr = eval(msg.list[i].timeLearn);
    		 var table= $('<table class="table table-striped table-hover table-bordered"></table>');
    		 table.append("<tr><th width='130px'>时间</th><th week='1'>周一</th><th  week='2'>周二</th><th  week='3'>周三</th><th  week='4'>周四</th><th  week='5'>周五</th><th  week='6'>周六</th><th  week='7'>周日</th></tr>")
    		 for(var j=0;j<arr.length;j++){
    			 var tr =$('<tr><td>'+arr[j].start+"--"+arr[j].end+'<br><span>'+arr[j].value+'</span></td><td week="1"></td><td  week="2"></td><td  week="3"></td><td  week="4"></td><td  week="5"></td><td  week="6"></td><td  week="7"></td></tr>')
    			 tr.children().attr("start",arr[j].start);
    			 tr.children().attr("end",arr[j].end);
    			 tr.children().attr("value",arr[j].value);
    			 tr.children().attr("classId",msg.list[i].table_id);
    			 table.append(tr);
    			 tr.children().click(function(){
    				 var v =$("#teacher_class").attr("value");
    				 if(!v||v.length<=0){
    					 alert("没有选择班级!")
    					 return;
    				 }
    				 if($(this).text().length>0){
    					 $(this).css({"background":"#fff"});
    					 $(this).text("");
    					 $(this).removeAttr("tid");
    				 }else{
    					 $(this).css({"background":"#438eb9"});
    					  var abcr = v.split("-");
        				 $(this).text(abcr[2]+"-"+abcr[3]);
        				 $(this).attr("tid",$("#teacher_class").attr('tid'));
        				 $(this).attr("sname",v);
    				 } 
    			 });
    		 }
    		$("#div"+table_id).append("<div style='width:100%;height:5px;'></div>"); 
    		$("#div"+table_id).append(table); 
    	 }  
    	 loadlearnTime();
	};
	//加载已有的表格
	function loadlearnTime(){ 
			
			var url ="${pageContext.request.contextPath}/edu/sys_ClassPlantAction.do?method=getTeacherPlantMaster";
			$.ajax({
			   type: "POST",
			   url: url,
			   async:false,
			   data: {table_id:id},
			   dataType:'json',
			   success: function(msg){
			      if(msg && msg.list && msg.list[0]!=null){
			    	  for(var i=0;i<msg.list.length;i++){
			    		  var yid ="td[start='"+msg.list[i].start1+"'][end='"+msg.list[i].end+"'][week='"+msg.list[i].week+"']";
			    		  $(yid).text(msg.list[i].sname);
			    		  $(yid).css({"background":"#438eb9"});
			    		  $(yid).attr("tid","${param.table_id}");
			    		  $(yid).attr("sname",msg.list[i].sname);
			    	  }
			      }
			   }
			}); 
	};
	//点击保存
	$("#saveClassplant").click(function(){
		var allclass = $("td[tid]");
		if(allclass.length<=0){
			alert("没有课程需要按排!");
			return;
		}
		var list = new Array();
		for(var i=0;i<allclass.length;i++){
			 var object = new Object();
			 object.start = $(allclass[i]).attr("start");
			 object.end = $(allclass[i]).attr("end");
			 object.value = $(allclass[i]).attr("value");
			 object.tid = $(allclass[i]).attr('tid');
			 object.week = $(allclass[i]).attr("week");
			 object.sname=$(allclass[i]).attr("sname");
			 list.push(object); 
		} 
		
		var url ="${pageContext.request.contextPath}/edu/sys_ClassPlantAction.do?method=addClassPlantMaster";
		var table_id = $("#teacher_class").attr("teacherId");
		if(table_id == ""){
			return;
		}
		$.ajax({
			   type: "POST",
			   url: url,
			   data:{table_id:table_id,arr:obj2str(list)},
			   dataType:'json',
			   success: function(msg){
			     alert(msg.message);
			   }
		});
	});
	//搜索的回车事件
	$('#serch_text_teacher').keydown(function(e){
		if(e.keyCode==13){
			$("#user_include").html("");
			var userName =$("#serch_text_teacher").val();
		 	$("#user_include").load("${pageContext.request.contextPath}/userWindow.jsp?r="+Math.random(),{userName:userName},function(){
		 		var dialog = $("#dialog-message").removeClass('hide').dialog({
					modal: true,
					title: "查询用户",
					width:800,
					close:function(){
						$(this).dialog('destroy'); 
						$("#user_include").html("");
					},
					title_html: true,
					buttons: [  
						{
							text: "提交",
							"class" : "btn btn-primary btn-xs",
							click: function() {
								var allc =$("input[type=checkbox][name=sys_user_table_checkbox]:checked");
								if(allc.length>1||allc.length<=0){
									alert("只能选中一行进行操作!");
									return;
								}
								var table_id=$(allc[0]).parent().parent().attr("id");
								var cdata =$("#"+table_id).data(table_id);
								id=cdata.table_id;
								loadingTachaerClas();
								$(this).dialog('destroy'); 
								$("#user_include").html("");
								$("#teacher_class").text(cdata.userShowName);
								$("#teacher_class").attr("teacherId",cdata.table_id);
							} 
						}
					]
				}); 
			}); 
			 
		}
	});
	$(function(){ 
		//$( "#tabs" ).tabs();
		loadingTachaerClas();
	});
})();
</script>